// Example express application adding the parse-server module to expose Parse
// compatible API routes.

var express = require('express')
var ParseServer = require('parse-server').ParseServer
var ParseDashboard = require('parse-dashboard')
var path = require('path')

const masterKey = '123456'
const appId = 'readhome' //测试环境
const serverURL = 'http://47.105.55.20:1337/parse' // 测试环境
// const appId = 'readhome-pro'// 正式环境
// const serverURL = 'http://47.105.55.20:1338/parse'// 正式环境

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI
databaseUri = 'mongodb://readhome:yanzi2018@dds-bp167487aa049f741227-pub.mongodb.rds.aliyuncs.com:3717,dds-bp167487aa049f742783-pub.mongodb.rds.aliyuncs.com:3717/readhome?replicaSet=mgset-8314371'

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.')
}

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://readhome:yanzi2018@dds-bp167487aa049f741227-pub.mongodb.rds.aliyuncs.com:3717,dds-bp167487aa049f742783-pub.mongodb.rds.aliyuncs.com:3717/readhome?replicaSet=mgset-8314371',
  cloud: process.env.CLOUD_CODE_MAIN || path.join(__dirname, '/cloud/main.js'),
  appId: process.env.APP_ID || appId,
  masterKey: process.env.MASTER_KEY || masterKey, // Add your master key here. Keep it secret!
  serverURL: process.env.SERVER_URL || serverURL, // Don't forget to change to https if needed
  liveQuery: {
    classNames: ['Posts', 'Comments'] // List of classes to support for query subscriptions
  }
})

// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express()

// 设置跨域访问
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'Origin,Authorization,X-Requested-With,Content-Type,Accept')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  // res.header("Content-Type", "application/json;charset=utf-8");
  // res.header('Content-Type', 'text/html');
  next()
})

// Serve static assets from the /public folder
app.use('/', express.static(path.join(__dirname, '/public')))

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse'
app.use(mountPath, api)

// wx developer check
app.get('/wxcheck', function (req, res) {
  res.send(req.query.echostr)
})

// wx pay notify_url
app.get('/wx/notify', require('./cloud/wxpay').wxnotify)
app.get('/wx/pay', require('./cloud/wxpay').wxpay)
// app.get('/wx/login', require('./cloud/wxlogin'))

// Parse Server plays nicely with the rest of your web routes
app.get('/', function (req, res) {
  res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!\n')
})

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function (req, res) {
  res.sendFile(path.join(__dirname, '/public/test.html'))
})

const dashboard = new ParseDashboard({
  apps: [{
    serverURL,
    appId,
    masterKey,
    appName: 'readhome'
  }],
  users: [{
    user: 'root',
    pass: 'root'
  }]
}, true)
app.use('/dashboard', dashboard)

var port = process.env.PORT || 80
var httpServer = require('http').createServer(app)
httpServer.listen(port, function () {
  console.log('parse-server-example running on port ' + port + '.')
})

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer)
